# -*- coding:utf-8 -*-
from aircos.db.core.sqlconn import get_conn


class APIResult(object):
    def __init__(self, result={}, code=True):
        self._result = result
        self._code = code

    def is_error(self):
        return not self._code

    def result(self):
        return self._result


def dec_make_conn_cursor(func, *args, **kwargs):
    def _func(*args, **kwargs):
        # make connection and cursor
        _conn = get_conn()

        if not _conn:
            #log.warn("get database connection timeout.")
            return APIResult(code=False)

        _cursor = _conn.cursor()
        if not (_conn and _cursor):
            #log.warn("cursor is none.")
            return APIResult(code=False)
        else:
            try:
                res = func(_conn, _cursor, *args, **kwargs)
            except Exception as e:
                #这里需要记录数据库错误
                print e
                return APIResult(code=False)
            else:

                _cursor.close()
                _conn.close()
                return res

    return _func
